<?php


class utilisateur {

	private $id;
	private $login;
	private $pwd;
	private $nom;
	private $prenom;
	private $email;
	private $tel;
	private $telEntreprise;
	private $entreprise;
	private $newsLetter;
	private $image;
	private $type;



	public function __construct(){
		$login = "";
		$pwd = "";
		$nom = "";
		$prenom = "";
		$email = "";
		$tel = "";
		$telEntreprise = "";
		$entreprise = "";
		$newsLetter = "";
	}

	public function getId() { return $this->id; }
	public function getLogin() { return $this->login; }
	public function getPwd() { return $this->pwd; }
	public function getNom() { return $this->nom; }
	public function getPrenom() { return $this->prenom; }
	public function getEmail() { return $this->email; }
	public function getTel() { return $this->tel; }
	public function getTelEntreprise() { return $this->telEntreprise; }
	public function getEntreprise() { return $this->entreprise; }
	public function getNewsLetter() { return $this->newsLetter; }
	public function getImage() { return $this->image; }
	public function getType() { return $this->type; }
	public function setId($x) { $this->id = $x; }
	public function setLogin($x) { $this->login = $x; }
	public function setPwd($x) { $this->pwd = $x; }
	public function setNom($x) { $this->nom = $x; }
	public function setPrenom($x) { $this->prenom = $x; }
	public function setEmail($x) { $this->email = $x; }
	public function setTel($x) { $this->tel = $x; }
	public function setTelEntreprise($x) { $this->telEntreprise = $x; }
	public function setEntreprise($x) { $this->entreprise = $x; }
	public function setNewsLetter($x) { $this->newsLetter = $x; }
	public function setImage($x) { $this->image = $x; }
	public function setType($x) { $this->type = $x; }

	static public function getByArray($Array) {
		if (is_array($Array)) {
			try {
				$result = new utilisateur();
				$result->setId(        (integer) $Array['ID']);
				$result->setLogin(     (string)  $Array['LOGIN']);
				$result->setPwd(   (string)  $Array['PWD']);
				$result->setNom(   (string)  $Array['NOM']);
				$result->setPrenom( (string) $Array['PRENOM']);
				$result->setEmail( (string) $Array['MAIL']);
				$result->setTel( (string) $Array['TEL']);
				$result->setTelEntreprise(   (string) $Array['TEL_ENTREPRISE']);
				$result->setNewsLetter(   (string) $Array['NEWSLETTER']);
				$result->setEntreprise(   (string) $Array['ENTREPRISE']);
				$result->setImage(   (string) $Array['IMAGE']);
				$result->setType(   (string) $Array['TYPE']);
			} catch (Exception $e) {
				$result = null;
			}
		} else {
			throw new InvalidArgumentException();
		}
		return $result;
	}

	public function getUserByLogin($login, $mdp){
		$DB = DB::getInstance();
		$result = null;
		$sql = "SELECT * ";
		$sql.= "FROM UTILISATEUR ";
		$sql.= "WHERE LOGIN = '".$login."'";
		$stm = $DB->query($sql);
		if($stm->rowCount()>0){
			try{
				$result = self::getByArray($stm->fetch(PDO::FETCH_ASSOC));
				if($result->getPwd() != $mdp)
					$result = null;
			}catch (Exception $e){
				$result = null;
			}
		}
		return $result;
	}
	
	public function getUserByID(){
		$DB = DB::getInstance();
		$result = null;
		$sql = "SELECT * ";
		$sql.= "FROM UTILISATEUR ";
		$sql.= "WHERE ID = '".$this->id."'";
		$stm = $DB->query($sql);
		if($stm->rowCount()>0){
			try{
				$result = self::getByArray($stm->fetch(PDO::FETCH_ASSOC));
				
			}catch (Exception $e){
				$result = null;
			}
		}
		return $result;
	}
	
	
	
	public function tryUserExist($login){
		$DB = DB::getInstance();
		$result = null;
		$sql = "SELECT * ";
		$sql.= "FROM UTILISATEUR ";
		$sql.= "WHERE LOGIN = '".$login."'";
		$stm = $DB->query($sql);
		if($stm->rowCount()>0){
			return true;
		}
		return false;
	}
	
	public function addUser(){
		$DB = DB::getInstance();
		$sql = "INSERT INTO UTILISATEUR (LOGIN, PWD, NOM, PRENOM, MAIL, TEL, TEL_ENTREPRISE, ENTREPRISE, NEWSLETTER) ";
		$sql.= "VALUES ('".$this->login."', '".$this->pwd."', '".$this->nom."', '".$this->prenom."', '".$this->email."', '".$this->tel."', '".$this->telEntreprise."', '".$this->entreprise."', '".$this->newsLetter."');";
		$stm = $DB->exec($sql);
		if($stm != false)
		return true;
		else return false;
	}
	
	public function updateUser(){
		$DB = DB::getInstance();
		$sql = "UPDATE UTILISATEUR SET LOGIN='".$this->login."', PWD='".$this->pwd."', NOM='".$this->nom."', PRENOM='".$this->prenom."', MAIL='".$this->email."', TEL='".$this->tel."', TEL_ENTREPRISE='".$this->telEntreprise."', ENTREPRISE='".$this->entreprise."', NEWSLETTER='".$this->newsLetter."' ";
		$sql.= "WHERE LOGIN='".$this->login."';";
		$stm = $DB->exec($sql);
		if($stm != false)
			return true;
		else return false;
	}	



}

?>